extern gxtkGraphics *bb_graphics_device;

static float alpha_threshold = 0.5;


class nDrawExts 
{

public: 
	static int Cls( float r,float g,float b, float a ){
		bb_graphics_device->primCount=0;
		glClearColor( r/255.0f,g/255.0f,b/255.0f,a/255.0f );
		glClear( GL_COLOR_BUFFER_BIT );

		return 0;
	}
	
	static int SetBlend( int blend ){
		bb_graphics_device->Flush();
		//hack hack hack  -nobu
		//2:  Soft Add;  3:  Multiply;  4:  Invert
		switch( blend ){
		case 1:
			glBlendFunc( GL_ONE,GL_ONE );
			break;
		case 2:
			glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE);
			break;		
		case 3:
			glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
			break;
		case 4:
			glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
			break;		
		//end hack
		default:
			glBlendFunc( GL_ONE,GL_ONE_MINUS_SRC_ALPHA );
		}

		return 0;
	}
	
	//Logic op blending.  Requires alpha threshold testing to work with textures in mojo...
	static int SetLogicOp( int logicop ){
		bb_graphics_device->Flush();
		
		if( (logicop > 0) && (glIsEnabled(GL_COLOR_LOGIC_OP) == GL_FALSE) ){
			glEnable( GL_COLOR_LOGIC_OP );
			glAlphaFunc(GL_GREATER, alpha_threshold);
			glEnable(GL_ALPHA_TEST);
		}	
		
		switch( logicop ){
		case 1: 
			glLogicOp(GL_COPY_INVERTED);
			break;
		case 2:
			glLogicOp(GL_INVERT);
			break;
		case 3:
			glLogicOp(GL_AND);
			break;
		case 4:
			glLogicOp(GL_NAND);
			break;
		case 5:
			glLogicOp(GL_OR);
			break;
		case 6:
			glLogicOp(GL_NOR);
			break;
		case 7:
			glLogicOp(GL_XOR);
			break;
		case 8:
			glLogicOp(GL_EQUIV);
			break;
		case 9:
			glLogicOp(GL_AND_REVERSE);
			break;
		case 10:
			glLogicOp(GL_AND_INVERTED);
			break;
		case 11:
			glLogicOp(GL_OR_REVERSE);
			break;
		case 12:
			glLogicOp(GL_OR_INVERTED);
			break;
		default:
			if( glIsEnabled(GL_COLOR_LOGIC_OP) == GL_TRUE ){
				glDisable( GL_COLOR_LOGIC_OP );
				glAlphaFunc(GL_ALWAYS,0);
				glDisable( GL_ALPHA_TEST );
			}
		}		
		
		return 0;
	}
	
	//Functions to deal with glAlphaFunc's testing threshold
	static int SetAlphaThreshold( float threshold ){
		alpha_threshold = threshold;
		return 0;
	}
	static float GetAlphaThreshold(){
		return alpha_threshold;
	}

	//Functions to mask off draws
	static int EnableStencil(){
		bb_graphics_device->Flush();
		glClearStencil(0x0);
	    glEnable(GL_STENCIL_TEST);				
		
		return 0;
	}

	static int ClearStencil(){
		bb_graphics_device->Flush();
	    glClear(GL_STENCIL_BUFFER_BIT);
		bb_graphics_device->Flush();
		return 0;
	}

	static int DrawToStencil(bool enabled){
		if ( enabled ){
				glAlphaFunc(GL_GREATER, alpha_threshold);
				glEnable(GL_ALPHA_TEST);
			
			glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);			
		    glStencilFunc (GL_ALWAYS, 1, 1);
    		glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
			
		} else {
			bb_graphics_device->Flush();
				glAlphaFunc(GL_ALWAYS,0);
				glDisable( GL_ALPHA_TEST );

			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);			
		    glStencilFunc (GL_EQUAL, 1, 1);
			glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
			
		}
		return 0;
	}

	static int DisableStencil(){
		bb_graphics_device->Flush();
		glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
		glDisable(GL_STENCIL_TEST);

		return 0;
	}
	
};

